package com.vfd.utils {
	import com.vfd.collections.iterartors.IIterator;
	
	/**
	* Returns a unique random item from the supplied list. In other words, it will return an item from 
	* the supplied list with out repeating an item in the seuqnce until all items have been returned once. 
	* If the loop property is set to true, the list will be recreated and the cycle will begin again. 
	* Implements the IIterartor interface so that it can be used in the place of an iterator since the
	* functionality is similar to an iterator, but the implementation of the class is different enough not
	* to be considered a subclass of iterator. 
	* @author Jeremy Wischusen <cortex@visualflowdesigns.com>
	* @since 3/24/2008
	*/
	public class UniqueRandomItem implements IIterator {
		private var _loop:Boolean = false; 
		private var _originalArray:Array; 
		private var _selectionArray:Array;
		public function UniqueRandomItem(...items) {
			this._originalArray = items;
			this._selectionArray = new Array(); 
			this.copyArray();
		}
		public function set loop(loop:Boolean):void {
			this._loop = loop;
		}
		
		private function copyArray():void {
			this._selectionArray.length = 0;
			for (var i:Number = 0; i < this._originalArray.length; i++) {
				this._selectionArray.push(this._originalArray[i]); 
			}
		}
		public function next():Object {
			if (this._loop && (this._selectionArray.length < 1)) {
				this.copyArray(); 
			}
			var randomIndex:Number = Math.floor(Math.random() * (this._selectionArray.length - 1) );
			var item:Array = this._selectionArray.splice(randomIndex, 1)
			trace("original array " + this._originalArray + ' selection array ' + this._selectionArray)
			return item[0]; 
		}
	}
	
}